package cn.yh.springbootjwt.controller;

import cn.yh.springbootjwt.common.AjaxResult;
import cn.yh.springbootjwt.controller.base.BaseController;
import cn.yh.springbootjwt.domain.entity.UserDo;
import cn.yh.springbootjwt.domain.query.LoginQuery;
import cn.yh.springbootjwt.domain.query.UserQuery;
import cn.yh.springbootjwt.domain.query.UserUpdate;
import cn.yh.springbootjwt.domain.vo.UserVo;
import cn.yh.springbootjwt.service.IUserService;
import cn.yh.springbootjwt.utils.JWTUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
@Api(tags = "用户管理接口")
public class UserController extends BaseController {

    @Autowired
    private IUserService userService;

    @ApiOperation(value = "测试接口")
    @GetMapping("/test")
    public String test() {
        return "UserController";
    }

    @ApiOperation(value = "登录接口")
    @PostMapping("/login")
    public AjaxResult login(@RequestBody @Validated LoginQuery loginQuery) {
        UserDo user = userService.loginByUsername(loginQuery.getUsername());
//        System.out.println(user);
        if (user == null) {
            return AjaxResult.error("你在我这没账号啊，赶紧去注册一个！");
        }
        if (!user.getPassword().equals(loginQuery.getPassword())){
            return AjaxResult.error("密码都能输错，试试123456？");
        }
        /*生成Token*/
        String token = JWTUtils.createToken(user);
//        System.out.println(token);
        /*设置Token有效期*/
        return AjaxResult.success( token,"登录成功");

    }
    // 新增 讲解CRUD标签的使用

    /*CRUD标签的使用
    * 1.查询当前用户的信息：@GetMapping("/info")
    * 2.创建新用户：@PostMapping("/create") 传参 username password
    * 3.更新当前用户的信息：@PutMapping("/update") 1
    * 4.用户注销账号：@DeleteMapping("/delete")
    * 除了创建新用户，其他的操作都需要通过Token解析UID，所以需要在请业务层中获取Token
    * */
    @ApiOperation(value = "查询当前用户信息")
    @GetMapping("/info")
    @ApiImplicitParam(name = "token", paramType = "header", required = true)
    public AjaxResult<UserVo> userInfo(){
        return AjaxResult.success(userService.getUserInfo());
    }

    @ApiOperation("创建新用户")
    @PostMapping("/create")
    @ApiImplicitParam(name = "token", paramType = "header", required = true)
    public AjaxResult create(@RequestBody UserQuery userQuery){
        return toAjax(userService.createUser(userQuery));
    }

    @ApiOperation("更新当前用户信息")
    @PutMapping("/update")
    @ApiImplicitParam(name = "token", paramType = "header", required = true)
    public AjaxResult update(@RequestBody UserUpdate userUpdate){
        return toAjax(userService.updateUser(userUpdate));
    }

    @ApiOperation("用户注销账号")
    @DeleteMapping("/delete")
    @ApiImplicitParam(name = "token", paramType = "header", required = true)
    public AjaxResult deleteUser() {
        return toAjax(userService.deleteUser());
    }
}
